<?php
require_once "includes/common.php";
require_once ('includes/header.php');
// pr($_POST);
  $msg='';
  $id = isset($_GET['id'])?$_GET['id']:0;

// for multiple delete of users
if(isset($_POST['selCheckToDel']))  {

  $ids = implode(',', $_POST['selCheckToDel']);

  $deleted = deleteByCondition($table='users'," `id` IN($ids)");
   $msg = "$deleted users deleted";
}

// for delete
if(isset($_GET['status'])) {
  $newStatus = ($_GET['status'] == 0 )? 1 : 0;

  $updateData = array('status'=>$newStatus);

  // update function is in common.php
  $isUpdated = update('users',$updateData," WHERE `id`=$id");
  if($isUpdated > 0){
    $msg =  "Status Changed";
  }
}// end if for delete

// for delete
if(isset($_GET['mode']) && $_GET['mode'] =='del') {


  $isDel = deleteById('users',$id);
  if($isDel > 0) {
    $msg =  "Record deleted successfully";
  }
}

// Fetch Users from database
$resource = fetchAll('users');
?>
<div class="hero-unit">
  <h4>Show All Users</h4>
</div>

<?php if($msg) {?>
  <div class="alert alert-block alert-success fade in">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <?=$msg?>
   </div>
<?php }?>

<div class="row-fluid">
<form name="deleteAll" method="post" action="">
  <table class="table table-hover">
    <thead>
      <tr>
        <th width="5%"><input type="checkbox" id="clickid" name="all"></th>
        <th width="5%">#</th>
        <th width="20%">Name</th>
        <th width="25%">Email</th>
        <th width="15%">DOB</th>
        <th width="15%">Avatar</th>
        <th width="10%">Status</th>
        <th width="10%">Options</th>
      </tr>
    </thead>
    <tbody>
<?php
  while ($data = mysql_fetch_assoc($resource)) {
    $id = $data['id'];
?>
      <tr>
         <td><input class="child" type="checkbox" name="selCheckToDel[]" value="<?=$id?>"></td>
         <td><a href="#"><?=$id?></a></td>
        <td><?=$data['uname']?></td>
        <td><a href="mailto:<?=$data['email']?>"><?=$data['email']?></a></td>
        <td><?php echo date('d-M-Y',strtotime($data['dob']));?></td>
        <td><img src="uploads/thumbs/<?=$data['avatar']?>"
           alt="<?=$data['avatar']?>"
          title="Users Image"></td>
        <td><a href=

"show_all_users.php?id=<?=$id?>&status=<?=$data['status']?>">


<?=getStatus($data['status'])?></td>
        <td>
          <a href="#" title="Edit"><i class="icon-edit"></i></a>
          <a href="show_all_users.php?mode=del&id=<?=$id?>" onClick="return confirm('Are you sure ?');" title="Delete"><i class="icon-trash"></i></a>
          <a target="_blank" href="view_profile.php?id=<?=$id?>" title="View"><i class="icon-user"></i></a>
        </td>
      </tr>
<?php }?>

    </tbody>
  </table>
  <input type="submit" name="delete" value="Delete Selected !!" class="btn btn-success btn-large">
  </form>
</div><!--/row-->
<?php
require_once ('includes/footer.php');
?>

 <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
  /////////////////////////////////////////
  $('#clickid').click(function() {
    var j=0;
    var checked = $(this).is(':checked');
    $('input[type="checkbox"].child ').each(function(i,j){
 //     j.checked = true;
      j.checked = checked;
    });
  });
  /////////////////////////////////////////////
  $('input[type="checkbox"].child').click(function(){
    var checked = $(this).is(':checked');
    if(checked == false){
      $('#clickid').removeAttr('checked');
    } else {
      var countCheckbox  = $('input[type="checkbox"].child').size();
      var countCheckedCheckbox  = $('input[type="checkbox"].child:checked').size();
      if(countCheckbox == countCheckedCheckbox){
        $('#clickid').attr('checked','true');
      }
    }
  });
});
</script>
<style type="text/css">
.table thead th {
    color:#FFF;
    background:#000;
    border-right:1px solid #FFF;
}
</style>
